<?php

    require_once( 'Database.php' );

    use PHPK\Database;

    $oDbh = new Database( dirname( dirname( __FILE__ ) ) .'/pdfbldr.db' );
    $oPdo = $oDbh->pdo;
    $oSql = null;
    $arSt = array();
    $strDocID = !empty( $_POST[ 'docid' ] ) ? $_POST[ 'docid' ] : uniqid();
    $strContent = !empty( $_POST[ 'content' ] ) ? $_POST[ 'content' ] : '';
    $nSaved = time();

    if ( !empty( $_POST[ 'docid' ] ) )
	$oSql = $oPdo->prepare( 'UPDATE pdfbldr SET content = :content, saved = :saved WHERE docid = :docid' );

    else
	$oSql = $oPdo->prepare( 'INSERT INTO pdfbldr( docid, content, saved ) VALUES( :docid, :content, :saved )' );

    $oSql->bindParam( ':docid', $strDocID );
    $oSql->bindParam( ':content', $strContent );
    $oSql->bindParam( ':saved', $nSaved );

    $arSt[ 'saved' ] = $oSql->execute() ? true : false;
    $arSt[ 'docid' ] = $strDocID;

    header( 'Content-Type: application/json' );
    print json_encode( $arSt );

?>
